package com.game.components
{
	import com.game.avatar.Animation;
	import com.game.avatar.Frame;

	import flash.geom.Point;

	public class AnimationComponent extends RenderComponent
	{
		protected var animation : Animation;

		protected var animationDelay : int;

		protected var runTimes : int;

		protected var targetPoint : Point;

		protected var playTime : int;

		protected var duration : int;

		protected var returnFun : Function;

		protected var executeFunTime : int;

		public var scaleX : int;

		/**
		 * 动画播放速度
		 */
		protected var playSpeed : Number = 0;

		public function AnimationComponent(type : * = null)
		{
			super(type == null ? AnimationComponent : type);
		}

		override public function notifyAdded() : void
		{
			super.notifyAdded();

			if (animation)
				filters = animation.filters;
		}

		public function setPoint(point : Point) : void
		{
			targetPoint = point;
		}

		/**
		 * 设置持续时间
		 * @param value
		 *
		 */
		public function setDuration(value : int) : void
		{
			duration = value;

			if (animation && duration > 0)
				animation.loops = 0;
		}

		/**
		 * 设置播放速度
		 * @param value
		 *
		 */
		public function setPlaySpeed(value : int) : void
		{
			playSpeed = value;
		}

		/**
		 * 设置动画播放多少时间后实行返回函数
		 * @param returnFun
		 * @param executeFunTime  0代表动画完毕后实行
		 *
		 */
		public function setReturnFun(returnFun : Function, executeFunTime : int = 0) : void
		{
			this.returnFun = returnFun;
			this.executeFunTime = executeFunTime;
		}

		public function setAnimation(animation : Animation, zIndex : int, playDelay : int = 0, loops : int = 0) : void
		{
			this.zIndex = zIndex;
			this.animation = animation;

			if (animation == null)
			{
				return;
			}
			animationDelay = playDelay;
			animation.loops = loops;
		}

		/**
		 * 重置
		 *
		 */
		public function reset() : void
		{
			if (animation)
			{
				animation.resetAnimation();
				runTimes = 0;
				playTime = 0;
			}
		}

		override public function update(delay : uint) : void
		{
			if (animation == null)
			{
				this.destroy();
				return;
			}
			runTimes += delay;

			if (runTimes > animationDelay)
			{
				animation.tryNext(delay, playSpeed);

				if (targetPoint == null)
				{
					updateRender(sceneObj.mapX, sceneObj.mapY, offsetX, offsetY);
				}
				else
				{
					updateRender(targetPoint.x, targetPoint.y, offsetX, offsetY);
				}

				if (duration > 0)
				{
					playTime += delay;

					if (playTime >= duration)
					{
						destroy();
					}
				}
			}

			if (executeFunTime > 0 && runTimes >= executeFunTime)
			{
				if (returnFun != null)
				{
					returnFun(owner);
					returnFun = null;
				}
			}

			if (animation && animation.loops == 1 && animation.isEnd)
			{
				destroy();
			}
		}

		override public function updateRender(sceneX : int, sceneY : int, offsetX : int = 0, offsetY : int = 0) : void
		{
			var frame : Frame = animation.curFrame;

			if (frame == null)
				return;
			render.bitmapData = frame.data;

			if (render.scaleX == 1)
			{
				render.x = sceneX + frame.offsetX + offsetX;
			}
			else
			{
				render.x = sceneX - frame.offsetX - offsetX;
			}

			render.y = sceneY + frame.offsetY + offsetY;
		}

		override public function destroy() : void
		{
			if (returnFun != null)
			{
				returnFun(owner);
				returnFun = null;
			}
			super.destroy();

			if (animation)
			{
				animation.destroy();
				animation = null;
			}
			targetPoint = null;
		}
	}
}